function Game(el){
this.el=document.querySelector(el)
this.map=new Map(this.el)
this.map.create()
this.food=new Food(this.el)
this.food.create()
this.snake=new Snake(this.el)
this.snake.create()


}
Game.prototype.start = function(){
    clearInterval(this.go)
   this.go=setInterval(()=>{
 
        this.snake.move()
        
       
        let snakeX = this.snake.snakeObj[0].x * 20
        let snakeY = this.snake.snakeObj[0].y * 20
        let foodX = this.food.foodX
        let foodY = this.food.foodY
        let sile=document.querySelector('.sile')
        let aniu=document.querySelector('.sile button')
        aniu.onclick=function(){
            location.reload()
        }
         if(snakeY<20) {
          sile.style.display='block'
          clearInterval(this.go)
          
    }
    if(snakeY>360) {
        sile.style.display='block'
        clearInterval(this.go)
        
  }
  if(snakeX>760){
    sile.style.display='block'
    clearInterval(this.go)
    
  }
  let snakeTou=this.snake.snakeObj[0]
       for(let i=1;i<this.snake.snakeObj.length;i++){
           if(snakeTou.x==this.snake.snakeObj[i].x&&snakeTou.y==this.snake.snakeObj[i].y){
            sile.style.display='block'
            clearInterval(this.go)
           }
       }
    if(snakeX<20) {
        sile.style.display='block'
        clearInterval(this.go)
        
  }
        
        if (snakeX == foodX && snakeY == foodY) {
          

            let last = this.snake.snakeObj[this.snake.snakeObj.length - 1]

            this.snake.snakeObj.push({
                x: last.x, 
                y : last.y, 
                color: last.color, 
                ele:null
            })
            this.snake.create() 
         
            this.food.move()
        }
    }, 150)

    document.addEventListener('keydown', (e) => {
      
        e = e || window.event
     
        switch (e.keyCode) {
            case 38:
                this.snake.direction = 'top'
                break;
            case 40:
                this.snake.direction = 'bottom'
                break;
            case 37:
                this.snake.direction = 'left'
                break;
            case 39:
                this.snake.direction = 'right'
                break;
            default:
                break;
        }
    })
}
Game.prototype.stop = function(){
    clearInterval(this.go)
 
}
